<template>
  <div class="user-profile">

    <van-nav-bar
    class="app-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <input
      type="file"
      hidden
      accept="image/*"
      ref="file"
      @change="onFileChange"
    >
    <van-cell
      title="头像"
      is-link
      center
      @click="$refs.file.click()" >
      <van-image
        width="30"
        height="30"
        round
        fit="cover"
        :src="user.photo"
      />
    </van-cell>
    <van-cell
      title="昵称"
      is-link
      :value="user.name"
      @click="isEditNameShow = true"
    />
    <van-cell
      title="性别"
      is-link
      :value="user.gender === 0 ? '男': '女' "
      @click="isEditGenderShow = true"
    />
    <van-cell
      title="生日"
      is-link
      :value="user.birthday"
      @click="isEditBirthdayShow=true"
    />

    <van-popup
      v-model="isEditNameShow"
      position="bottom"
      :style="{ height: '100%'}"
    >
      <update-name
        v-if="isEditNameShow"
        v-model="user.name"
        @close="isEditNameShow = false"
      />
    </van-popup>

    <van-popup
      v-model="isEditGenderShow"
      position="bottom"
    >
      <update-gender
      v-model="user.gender"
        @close="isEditGenderShow=false" />
    </van-popup>

    <van-popup
      v-model="isEditBirthdayShow"
      position="bottom"
    >
    <update-birthday
      v-if="isEditBirthdayShow"
      v-model="user.birthday"
      @close="isEditBirthdayShow=false"
    />
    </van-popup>

    <van-popup
      class="update-photo-popup"
      v-model="isEditPhotoShow"
      position="bottom"
      style="height:100%"
    >
      <update-photo
        v-if="isEditPhotoShow"
        :file="previewImage"
        @close="isEditPhotoShow=false"
        @update-photo="user.photo=$event"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import updateName from './components/update-name'
import UpdateGender from './components/update-gender'
import UpdateBirthday from './components/update-birthday'
import UpdatePhoto from './components/update-photo'

export default {
  name: 'UserProfile',
  components: {
    updateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  },
  data () {
    return {
      user: {},
      isEditNameShow: false,
      isEditGenderShow: false,
      isEditBirthdayShow: false,
      isEditPhotoShow: false,
      previewImage: null
    }
  },
  mounted () {

  },
  created () {
    this.loadUserProfile()
  },
  methods: {
    async loadUserProfile () {
      const { data } = await getUserProfile()
      this.user = data.data
    },
    onFileChange () {
      this.isEditPhotoShow = true
      const file = this.$refs.file.files[0]
      // const blob = URL.createObjectURL(this.$refs.file.files[0])
      this.previewImage = file
      this.$refs.file.value = ''
    }
  }
}
</script>

<style lang="less" >
  .van-popup {
    background-color: #f5f7f9;
  }
  .update-photo-popup{
    background-color: #000;
  }
</style>
